﻿using System.Linq.Expressions;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.EventTemplate.Model;
using Basic.FlowRemoteModel.Flow.Model;
using Basic.FlowRemoteModel.FlowApp.Model;
using Basic.FlowRemoteModel.FlowAudit.Model;
using LinqKit;
using SqlSugar;
using WeDonekRpc.Helper;

namespace Basic.FlowDAL.lmpl
{
    internal static class WhereLinq
    {
        public static Expression<Func<DBFlowAuditLog, bool>> ToWhere ( this AuditLogQuery query )
        {
            ExpressionStarter<DBFlowAuditLog> where = PredicateBuilder.New<DBFlowAuditLog>();
            if ( query.AppId.HasValue )
            {
                where = where.And(a => a.AppId == query.AppId.Value);
            }
            if ( !query.AuditStatus.IsNull() )
            {
                where = where.And(a => query.AuditStatus.Contains(a.AuditStatus));
            }
            if ( query.Status.HasValue )
            {
                where = where.And(a => a.Status == query.Status.Value);
            }
            if ( query.Begin.HasValue && query.End.HasValue )
            {
                where = where.And(a => SqlFunc.Between(a.CreateTime, query.Begin.Value, query.End.Value));
            }
            else if ( query.Begin.HasValue )
            {
                where = where.And(a => a.CreateTime >= query.Begin.Value);
            }
            else if ( query.End.HasValue )
            {
                where = where.And(a => a.CreateTime <= query.End.Value);
            }
            if ( query.ToEmpId.HasValue )
            {
                where = where.And(a => SqlFunc.Subqueryable<DBFlowAuditItemLog>().Where(c => c.LogId == a.Id && c.EmpId == query.ToEmpId.Value).Any());
            }
            if ( !query.QueryKey.IsNull() )
            {
                where = where.And(a => a.DocTitle.Contains(query.QueryKey));
            }
            if ( where.IsStarted )
            {
                return where;
            }
            return null;
        }
        public static Expression<Func<DBFlowList, bool>> ToWhere ( this FlowQueryParam query )
        {
            ExpressionStarter<DBFlowList> where = PredicateBuilder.New<DBFlowList>(a => a.SourceId == null);
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.FlowName.Contains(query.QueryKey));
            }
            if ( !query.FlowType.IsNull() )
            {
                where = where.And(a => a.FlowType == query.FlowType);
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            return where;
        }
        public static Expression<Func<DBFlowApp, bool>> ToWhere ( this FlowAppQuery query )
        {
            ExpressionStarter<DBFlowApp> where = PredicateBuilder.New<DBFlowApp>();
            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.AppName.Contains(query.QueryKey));
            }
            if ( !query.AppClassify.IsNull() )
            {
                where = where.And(a => a.AppClassify == query.AppClassify);
            }
            if ( query.AppType.HasValue )
            {
                where = where.And(a => a.AppType == query.AppType.Value);
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            if ( !where.IsStarted )
            {
                return null;
            }
            return where;
        }
        public static Expression<Func<DBFlowEventTemplate, bool>> ToWhere ( this InterQueryParam query )
        {
            ExpressionStarter<DBFlowEventTemplate> where = PredicateBuilder.New<DBFlowEventTemplate>();

            if ( query.QueryKey.IsNotNull() )
            {
                where = where.And(a => a.InterName.Contains(query.QueryKey));
            }
            if ( query.InterType.HasValue )
            {
                where = where.And(a => a.InterType == query.InterType.Value);
            }
            if ( !query.UseRange.IsNull() )
            {
                where = where.And(a => query.UseRange.Contains(a.UseRange));
            }
            if ( !query.Status.IsNull() )
            {
                where = where.And(a => query.Status.Contains(a.Status));
            }
            if ( !where.IsStarted )
            {
                return null;
            }
            return where;
        }
    }
}
